package com.momoauth.api_client.controller;

import com.momoauth.api_client.config.SmsConfig;
import com.momoauth.common.core.result.ResponseResurt;
import com.momoauth.common.tps.aliyun.service.AliyunSmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/sms")
@Api(tags = "阿里云短信")
public class SmsController {

    /**
     * 发送短信
     * @param phoneNumbers
     * @param templateParam
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @ApiOperation(value = "发送短信")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneNumbers", value = "手机号码", required = true),
            @ApiImplicitParam(name = "templateParam", value = "模板参数", required = true)
    })
    @RequestMapping(value = "/sendSMS", method = RequestMethod.GET)
    public ResponseResurt sendSMS(
            String phoneNumbers,
            String templateParam
    ) throws ExecutionException, InterruptedException {
        // 构建参数
        Map<String, String> templateMap = new HashMap<>();
        templateMap.put("signName", SmsConfig.signName);
        templateMap.put("templateCode", SmsConfig.templateCode);
        templateMap.put("templateParam", templateParam);
        // 调用阿里云短信服务
        AliyunSmsService aliyunSmsService = new AliyunSmsService(
                SmsConfig.accessKeyID,
                SmsConfig.accessKeySecret
        );
        // 判断是否发送成功
        if (aliyunSmsService.sendSMS(phoneNumbers,templateMap)) {
            return ResponseResurt.success();
        }
        return ResponseResurt.error();
    }
}
